/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-10-16 09:55:48
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-11-13 11:58:07
 * @FilePath: \cxl-admin\src\pages\finance\reconciliationCollect\detailed\index.ts
 */
import { reactive, ref } from 'vue'
import * as tableConfig from './config.json'
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { ElMessage } from 'element-plus'
import {
    paramsReconciliationInterface,
    reconciliationDetailedList,
    reconciliationTotal
} from '@/apis/finance/reconciliation'
import { CreateXMLHttpRequest } from '@/utils'

export default class ConsumeDetailedData extends BaseData implements BaseMethod {
    dialog = reactive({
        visible: false,
        title: '',
        width: '70%',
        condition: '',
        customId: ''
    })
    fromTo = reactive([])
    companyId = ref('')
    constructor() {
        super(tableConfig)
    }
    getData = (arg?: any) => {
        if (this.uStore.isSuperAdmin || Number(this.uStore.companyId) === 1) {
            const params: paramsReconciliationInterface = {
                companyId: this.companyId.value,
                current: this.pageData.pagination.pageNo,
                size: this.pageData.pagination.pageSize,
                queryDateStart: this.fromTo[0],
                queryDateEnd: this.fromTo[1]
            }
            reconciliationDetailedList(params).then(res => {
                if (res.data.value === 200) {
                    this.pageData.table.data.length = 0
                    if (res.data.data.records.length) {
                        this.dialog.condition = res.data.data.records[0].dataDate + '~' + this.fromTo[1]
                        this.pageData.table.data.push({
                            dataDate: res.data.data.records[0].dataDate,
                            storeAmount: '-',
                            realTimeStoreAmount: '-',
                            maintenanceSearchAmount: '-',
                            crashSearchAmount: '-',
                            powerBatterySearchAmount: '-',
                            refundAmount: '-',
                            endRemainStoreAmount: res.data.data.records[0].primaryRemainStoreAmount || '0.00'
                        })
                    }

                    res.data.data.records.forEach((item: any) => {
                        item.storeAmount = item.storeAmount || '0.00'
                        item.realTimeStoreAmount = item.realTimeStoreAmount || '0.00'
                        item.maintenanceSearchAmount = item.maintenanceSearchAmount || '0.00'
                        item.crashSearchAmount = item.crashSearchAmount || '0.00'
                        item.powerBatterySearchAmount = item.powerBatterySearchAmount || '0.00'
                        item.refundAmount = item.refundAmount || '0.00'
                        item.endRemainStoreAmount = item.endRemainStoreAmount || '0.00'

                        this.pageData.table.data.push(item)
                    })

                    this.pageData.pagination.total = res.data.data.total
                } else {
                    ElMessage.error(res.data.desc)
                }
            })
            reconciliationTotal(params).then(res => {
                if (res.data.value === 200) {
                    this.pageData.table.summaryArray = [
                        '小计', //占2列
                        '',
                        res.data.data.realTimeStoreAmountSum || '0.00', //实时充值
                        res.data.data.maintenanceSearchAmountSum || '0.00', //维保车况
                        res.data.data.crashSearchAmountSum || '0.00', //出险车况
                        res.data.data.powerBatterySearchAmount || '0.00', //新能源车况
                        res.data.data.refundAmountSum || '0.00', //退费
                        res.data.data.storeAmountSum || '0.00' //充值余额
                    ]
                } else {
                    ElMessage.error(res.data.desc)
                }
            })
        }
    }
    onSearch = (arg?: any) => {}
    onReset = (arg?: any) => {}
    onPaginationChange = (arg?: any) => {
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.getData()
    }
    onOperation = (arg?: any) => {}
    onExport = () => {
        if (!this.pageData.table.data.length) {
            return
        }
        const params = `current=99&size=99&companyId=${this.companyId.value}&queryDateStart=${this.fromTo[0]}&queryDateEnd=${this.fromTo[1]}`
        console.log('params', params)
        const url =
            import.meta.env.VITE_HOST + '/api/mall/company/dayStatistics/checkAccountDetailPageListExport?' + params

        CreateXMLHttpRequest(url, this.dialog.title + '对账汇总明细列表')
    }
}
